package com.zhanlv.composer

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.zhanlv.composer.databinding.ActivityMainBinding
import org.json.JSONObject

class MainActivity : AppCompatActivity() {
    var index = 0

    lateinit var viewBinding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)

        viewBinding.startServer.setOnClickListener {
            SocketServer.start(6667)
        }

        viewBinding.connectToServer.setOnClickListener {
            SocketClient.connect("127.0.0.1", 6667)
        }

        viewBinding.request.setOnClickListener {
            var params = JSONObject()
            params.put("module", "user")
            params.put("action", "add")
            params.put("params", "{\"name\":\"wooran\",\"age\":40,\"index\":$index}")

            SocketClient.request(params.toString())

            index += 1
        }
    }
}